package com.lwj.hiring.company.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lwj.hiring.common.util.AppUtil;
import com.lwj.hiring.company.dao.ApplicationDao;
import com.lwj.hiring.company.dao.PositionDao;
import com.lwj.hiring.company.entity.ApplicationEntity;
import com.lwj.hiring.company.entity.PositionEntity;
import com.lwj.hiring.talent.dao.ResumeDao;
import com.lwj.hiring.talent.entity.ResumeEntity;

/**
 * PositionServlet
 * 
 * @author Administrator
 *
 */
public class PositionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PositionDao positionDao = new PositionDao();
	private ResumeDao resumeDao = new ResumeDao();
	private ApplicationDao applicationDao = new ApplicationDao();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("queryAll".equals(command)) {
			queryAll(request, response);
		} else if ("detail".equals(command)) {
			detail(request, response);
		} else if ("add".equals(command)) {
			add(request, response);
		} else if ("delete".equals(command)) {
			delete(request, response);
		} else if ("preupdate".equals(command)) {
			preupdate(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else {
			queryAll(request, response);
		}
	}

	/**
	 * 删除职位
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		int row = positionDao.delete(id);
		response.sendRedirect("CompanyServlet?command=main&row=" + row);
	}

	/**
	 * 发布招聘职位
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		int companyId = AppUtil.getCompanyId(request, response);
		String name = request.getParameter("name");
		String duty = request.getParameter("duty");
		String ability = request.getParameter("ability");
		String salary1 = request.getParameter("salary1");
		String salary2 = request.getParameter("salary2");
		List<Object> list = new ArrayList<>();
		list.add(companyId);
		list.add(name);
		list.add(duty);
		list.add(ability);
		list.add(salary1);
		list.add(salary2);
		int row = positionDao.add(list);
		response.sendRedirect("CompanyServlet?command=main&row=" + row);
	}

	/**
	 * 职位详情
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void detail(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if (AppUtil.isLogin(request)) {
			// 职位详情
			int id = Integer.parseInt(request.getParameter("id"));
			PositionEntity position = positionDao.detail(id);
			request.setAttribute("position", position);
			// 某求职者所有简历列表
			int talentId = AppUtil.getTalentId(request, response);
			List<ResumeEntity> resumeList = resumeDao.list(talentId);
			request.setAttribute("resumeList", resumeList);
			// 验证此职位是否已经投递
			ApplicationEntity application = applicationDao
					.isApply(talentId, id);
			request.setAttribute("application", application);

			request.getRequestDispatcher("view/company/position-detail.jsp")
					.forward(request, response);
		} else {
			request.setAttribute("message", "请先登录，再执行简历投递！");
			request.getRequestDispatcher("view/talent/talent-login.jsp")
					.forward(request, response);
		}
	}

	/**
	 * 所有职位列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void queryAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<PositionEntity> positionList = positionDao.queryAll();
		request.setAttribute("positionList", positionList);
		request.getRequestDispatcher("view/company/position-list.jsp").forward(
				request, response);
	}

	/**
	 * 职位修改（查询之前信息，显示到修改页面，方便后期参照修改）
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void preupdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 1、获取前台传过来的参数
		int id = Integer.parseInt(request.getParameter("id"));
		// 2、调用业务逻辑
		PositionEntity position = positionDao.queryById(id);
		// 3、转向
		if (position == null) {
			request.setAttribute("error", "该职位不存在~");
			response.sendRedirect("CompanyServlet?command=main");
		} else {
			request.setAttribute("position", position);
			request.getRequestDispatcher("view/company/position-update.jsp")
					.forward(request, response);
		}
	}

	/**
	 * 修改职位信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		int companyId = AppUtil.getCompanyId(request, response);
		int positionId = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String duty = request.getParameter("duty");
		String ability = request.getParameter("ability");
		String salary1 = request.getParameter("salary1");
		String salary2 = request.getParameter("salary2");
		List<Object> list = new ArrayList<>();
		list.add(companyId);
		list.add(name);
		list.add(duty);
		list.add(ability);
		list.add(salary1);
		list.add(salary2);
		list.add(positionId);
		int row = positionDao.update(list);
		response.sendRedirect("CompanyServlet?command=main&row=" + row);
	}
}
